#include "LedDrv.h"

#define GPBCON     (*(volatile unsigned long*)0x56000010)
#define GPBDATA    (*(volatile unsigned long*)0x56000014)


#define LED1_MASK  (1 << 5)
#define LED2_MASK  (1 << 6)
#define LED3_MASK  (1 << 7)
#define LED4_MASK  (1 << 8)


void set_led(int led_no, int is_set_on) {
    switch(led_no) {
        case 1:
            if (1 == is_set_on) {
                GPBDATA &= ~LED1_MASK; //set bit5 to 0, to let led1 on
            } else {
                GPBDATA |= LED1_MASK; //set bit5 to 1, to let led1 off
            }
            break;
        case 2:
            if (1 == is_set_on) {
                GPBDATA &= ~LED2_MASK; //set bit6 to 0, to let led2 on
            } else {
                GPBDATA |= LED2_MASK; //set bit6 to 1, to let led2 off
            }
            break;
        case 3:
            if (1 == is_set_on) {
                GPBDATA &= ~LED3_MASK; //set bit7 to 0, to let led3 on
            } else {
                GPBDATA |= LED3_MASK; //set bit7 to 1, to let led3 off
            }
            break;
        case 4:
            if (1 == is_set_on) {
                GPBDATA &= ~LED4_MASK; //set bit8 to 0, to let led4 on
            } else {
                GPBDATA |= LED4_MASK; //set bit8 to 1, to let led4 off
            }
            break;
        default:
            if (1 == is_set_on) {
                GPBDATA &= ~LED1_MASK; //set bit5 to 0, to let led1 on
                GPBDATA &= ~LED2_MASK; //set bit5 to 0, to let led1 on
                GPBDATA &= ~LED3_MASK; //set bit5 to 0, to let led1 on
                GPBDATA &= ~LED4_MASK; //set bit5 to 0, to let led1 on
            } else {
                GPBDATA |= LED1_MASK; //set bit5 to 1, to let led1 off
                GPBDATA |= LED2_MASK; //set bit5 to 1, to let led1 off
                GPBDATA |= LED3_MASK; //set bit5 to 1, to let led1 off
                GPBDATA |= LED4_MASK; //set bit5 to 1, to let led1 off
            }
            break;
    }
}

void init_led() {
    GPBCON |= (0x55 << 10); //set GPB5(led1), GPB6(led2), GPB7(led3), GPB8(led4) as output port
    set_led(LED_ALL, LED_OFF); //set all led off
}


void set_led_by_low_4bit(unsigned char info) {
    int i = 0;

    if (0 == (info & 0xF)) {
        set_led(LED_ALL, LED_ON);
        return;
    }

    for (i = 0; i < 4; ++i) {
        if (info & (1 << i)) {
            set_led(i + 1, LED_ON);
        } else {
            set_led(i + 1, LED_OFF);
        }
    }
}








